package uplift.project;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class UserCreatedEventActivity extends Activity {
	
	private String name;
	private String category;
	private String location;
	private String date;
	private String startTime;
	private String endTime;
	private String description;
	
	private TextView eventName;
	private TextView eventCategory;
	private TextView eventDate;
	private TextView eventLocation;
	private TextView eventTime;
	private TextView eventDescription;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_created_event);
        
        Intent startIntent = this.getIntent();
        Bundle extras = startIntent.getExtras();
        name = extras.getString("name");
        category = extras.getString("category");
        location = extras.getString("location");
        date = extras.getString("date");
        startTime = extras.getString("starts");
        endTime = extras.getString("ends");
        description = extras.getString("description");

        eventName = (TextView) this.findViewById(R.id.textView_previewEventName);
        eventCategory = (TextView) this.findViewById(R.id.textView_previewCategory);
        eventDate = (TextView) this.findViewById(R.id.textView_previewDate);
        eventLocation = (TextView) this.findViewById(R.id.textView_previewLocation);
        eventTime = (TextView) this.findViewById(R.id.textView_previewTime);
        eventDescription = (TextView) this.findViewById(R.id.textView_previewDescription);
        
        eventName.setText(name);
        eventCategory.setText(category);
        eventDate.setText(date);
        eventLocation.setText(location);
        eventTime.setText(startTime + " - " + endTime);
        eventDescription.setText(description);
        eventDescription.setMovementMethod(new ScrollingMovementMethod());
        
		// set the action bar to have up-navigation enabled
	    ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    // Goes to edit event screen
    public void editEvent(View v) {
	   	Intent i = new Intent(v.getContext(), CreateEventActivity.class);

	   	i.putExtra("name", name);
	   	i.putExtra("category", category);
	   	i.putExtra("location", location);
	   	i.putExtra("date", date);
	   	i.putExtra("starts", startTime);
	   	i.putExtra("ends", endTime);
	   	i.putExtra("description", description);
		
	   	startActivity(i);
    }
    
    // Goes to delete event screen
    public void deleteEvent(View v) {
    	alertbox("Delete this event?", "Are you sure you want to delete the event " + name + "?");
    }
    
    protected void alertbox(final String title, final String mymessage)
    {
    new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog))
       .setMessage(mymessage)
       .setTitle(title)
       .setPositiveButton("Yes",
    		   new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   confirmBox("Event Deleted", "You successfully deleted the event " + name + ".");
		           }
		       })
       .setNegativeButton(android.R.string.cancel,
    		   new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		           }
		       })
       .show();
    }
    
    protected void confirmBox(String title, String message) {
    	new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog))
        .setMessage(message)
        .setTitle(title)
        .setPositiveButton("OK",
     		   new DialogInterface.OnClickListener() {
 		           public void onClick(DialogInterface dialog, int whichButton){
		        	   ApplicationData appState = ((ApplicationData)UserCreatedEventActivity.this.getApplication());
		        	   appState.eventsList.removeEvent(name, category, location, date, startTime, endTime, description);
		        	   
			           	// Direct to confirmation page after deleting
			           	Intent i = new Intent(UserCreatedEventActivity.this.getApplicationContext(), UpliftActivity.class);
			           	i.putExtra("name", name);
			           	startActivity(i);
 		           }
 		       })
 		.show();
    }
    
    // handles action item button presses
  	@Override
  	public boolean onOptionsItemSelected(MenuItem item) {
  		if(item.getItemId() == android.R.id.home) {
  			// move up the hierarchy

  			Intent i = new Intent(this.getApplicationContext(), MyEventsTabActivity.class);
  			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  			startActivity(i);
  		} else if(item.getItemId() == R.id.menu_home) {
  			// go to Home screen
  			
  			Intent i = new Intent(this.getApplicationContext(), UpliftActivity.class);
  			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  			startActivity(i);
  		} else if(item.getItemId() == R.id.menu_my_events) {
  			// go to My Events screen
  			
  			Intent i = new Intent(this.getApplicationContext(), MyEventsTabActivity.class);
  			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  			startActivity(i);
  		}
  		return true;
  	}
  	
  	// set up Action Bar
  	@Override
  	public boolean onCreateOptionsMenu(Menu menu) {
  	    MenuInflater inflater = getMenuInflater();
  	    inflater.inflate(R.menu.action_bar, menu);
  	    return true;
  	}
    
    

}